/**
 * Created by rong on 16/9/6.
 */
require.config(require_config);
define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function ($, common, ready, Collection, template, Model, dialog, datepicker) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },
        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'site/promotion',
                condition: {},
                model: {
                    url: _global.url.api + 'site/promotion',
                    event: {
                        '.item-edit:click': 'add',
                        '.item-remove:click': 'remove',
                    },
                    add: function (e) {
                        _this.data = this.data;
                        var model = this;
                        e.preventDefault();
                        _this.dialog = dialog({
                            title: '活动报名',
                            content: template('add-tpl', this.data),
                            width: 700,
                            ok: function () {
                                if (_this.data.active_name == '') {
                                    Alert({msg: '请输入活动名称', type: 'error'});
                                    return false;
                                }

                                _this.data.type = $("#type").val();
                                if (_this.data.type < 1) {
                                    Alert({msg: '版块不能为空', type: 'error'});
                                    return false;
                                }
                                _this.data.id = _this.data.id;
                                _this.data.goods_id = $('#keyword').val();
                                _this.data.title = $('#title').val();
                                _this.data.active_time = $('#dt_end').val();
                                _this.data.dt_start = $('#dt_start').val();
                                _this.data.sku_time = $('#expired_at_edit').val();
                                _this.data.sku_start_time = $('#started_at_edit').val();
                                _this.data.img = $("input[name='imgs']").val();

                                var date1 = new Date(Date.parse(_this.data.active_time));
                                var date2 = new Date(Date.parse(_this.data.sku_time));
                                if (date1.getTime() > date2.getTime()) {
                                    Alert({msg: 'sku价格结束时间不能小于活动的结束时间,请重新设置sku时间', type: 'error'});
                                    return false;
                                }
                                var dates1 = new Date(Date.parse(_this.data.dt_start));
                                var dates2 = new Date(Date.parse(_this.data.sku_start_time));
                                if (dates1.getTime() < dates2.getTime()) {
                                    Alert({msg: 'sku价格开始时间不能大于活动的开始时间,请重新设置sku时间', type: 'error'});
                                    return false;
                                }
                                if (_this.data.img == '') {
                                    Alert({msg: '请上传图片', type: 'error'});
                                    return false;
                                }
                                if (model.post()) {
                                    _this.collection.reload();
                                }
                                ;
                            }, cancel: function () {
                            }
                        });
                        _this.dialog.show();
                        $('.long_date').on('click', function () {
                            WdatePicker({
                                el: this.id,
                                dateFmt: 'yyyy-MM-dd HH:mm:ss'
                            });
                        });
                    },
                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        },
        event: function () {
            this.data = {};
            var _this = this;
            $('#btn-search').on('click', function (e) {
                e.preventDefault();
                _this.collection.condition.type = $("#type").val();
                _this.collection.condition.start_time = $('input[name="start_time"]').val();
                _this.collection.condition.end_time = $('input[name="end_time"]').val();
                _this.collection.reload();
            });

            $('#btn-status').find('a').on('click', function (e) {
                e.preventDefault();
                $("#type").val('all');
                $('input[name="start_time"]').val('');
                $('input[name="end_time"]').val('');
                _this.collection.condition.type = $("#type").val();
                _this.collection.condition.start_time = $('input[name="start_time"]').val();
                _this.collection.condition.end_time = $('input[name="end_time"]').val();
                _this.collection.condition.status = $(e.target).attr('rel');
                _this.collection.reload();
                $(e.target).addClass('active').siblings().removeClass('active');
            });

            $('body').on('click', '#btn-goods-search', function (e) {
                goods_id = $('#keyword').val();
                if (goods_id == '') {
                    Alert({msg: '请填写商品id进行搜索', type: 'error'});
                    return false;
                }
                $.ajax({
                    url: _global.url.api + 'goods_sku_info',
                    data: {goods_id: goods_id},
                    type: 'get',
                    async: false,
                    success: function (res) {
                        if (res.code == 0) {
                            $('#sku_info').html(template('edit-tpl', res.data));
                            //console.log(res.data);
                            $('.long_date').on('click', function () {
                                WdatePicker({
                                    el: this.id,
                                    dateFmt: 'yyyy-MM-dd HH:mm:ss'
                                });
                            });
                        } else {
                            Alert({msg: res.msg, type: 'error'});
                            return false;
                        }
                    }
                });
            });
            $('input,select,textarea').on('change', function (e) {
                _this.change(e);
            });
            //上传图片
            $('body').on('click', '.btn-upload', function (e) {
                e.preventDefault();
                var item = $(this).attr("attr");
                _this.upload_dialog = dialog({
                    content: template('upload-tpl', {}),
                    width: 550,
                    ok: function () {
                        var img = $("#upload-frame")[0].contentWindow.image.get();
                        img.item = item;
                        _this.imgPreview(img);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog.show();
            });


            //保存
            $('.long_date').on('click', function () {
                WdatePicker({
                    el: this.id,
                    dateFmt: 'yyyy-MM-dd HH:mm:ss'
                });
            });

        },
        imgPreview: function (img) {
            //console.log('this.data', this.data);
            if (typeof img.id !== 'undefined') {
                this.data.img = img.id;
                $('#img-view' + img.item).html(template('img-tpl', img));
            }
        }
    };

    new index();
});